//author Dreena Biswas

package view;
import model.*;
import controller.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.util.*;



public class NewFrame {
	
	public static JLabel fName = new JLabel("First Name");
	public static JTextField fNameEntry = new JTextField(10);
	public static JLabel lName = new JLabel("Last Name");
	public static JTextField lNameEntry = new JTextField(10);
	public static JLabel group = new JLabel("Group");
	public static JTextField groupEntry = new JTextField(10);
	public static JLabel homeAdd = new JLabel("Home Address");
	public static JTextField homeAddEntry = new JTextField(10);
	public static JLabel compAdd = new JLabel("Company Address");
	public static JTextField compAddEntry = new JTextField(10);
	public static JLabel personalMobile = new JLabel("Personal Mobile");
	public static JTextField personalMobileEntry = new JTextField(10);
	public static JLabel personalLand = new JLabel("Personal Land");
	public static JTextField personalLandEntry = new JTextField(10);
	public static JLabel profMobile = new JLabel("Professional Mobile");
	public static JTextField profMobileEntry = new JTextField(10);
	public static JLabel profLand = new JLabel("Professional Land");
	public static JTextField profLandEntry = new JTextField(10);
	public static JLabel homePage = new JLabel("Home Page Link");
	public static JTextField homePageEntry = new JTextField(10);
	public static JLabel emailId = new JLabel("Email id");
	public static JTextField emailIdEntry = new JTextField(10);
	public static JLabel photo = new JLabel("Photo");
	public static JTextField photoEntry = new JTextField(10);
	public static JLabel date = new JLabel("Date");
	public static JTextField dateEntry = new JTextField(10);
	public static JLabel letter = new JLabel("Sort by");
	public static JTextField letterEntry = new JTextField(10);
	public static JButton submitButton = new JButton("Submit");
	public static JButton submitButton1 = new JButton("Modify");
	public static JButton submitButton2 = new JButton("Sort");
	public static JButton submitButton3 = new JButton("delete");
	public static JButton submitButton4 = new JButton("add");
	public static JList list; 
	/*final BufferedImage image = ImageIO.read(NewFrame.class.getClassLoader().getResource("img/user.png"));
	 protected void paintComponent(Graphics g) {
	        super.paintComponent(g);
	        int centerPoint = (getHeight() - image.getHeight())/2;
	        g.drawImage(image, 0, centerPoint, this);
	    }
	}
	//look for ImageIcon*/
	
	//ADD
	static JPanel addPanel = new JPanel();
	private static JFrame addframe = new JFrame("Add Contact");
	public static void createFrame1(){
		
		
		getAddframe().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getAddframe().setSize(600, 200);
		getAddframe().setLocationRelativeTo(null);
		//photoEntry.setMargin(new Insets(0, image.getWidth(), 0, 0));
		//photoEntry.setPreferredSize(new Dimension(textField.getWidth(), image.getHeight() + 10));

		addPanel.add(fName);
		addPanel.add(fNameEntry);
		addPanel.add(lName);
		addPanel.add(lNameEntry);
		addPanel.add(group);
		addPanel.add(groupEntry);
		addPanel.add(homeAdd);
		addPanel.add(homeAddEntry);
		addPanel.add(compAdd);
		addPanel.add(compAddEntry);
		addPanel.add(personalMobile);
		addPanel.add(personalMobileEntry);
		addPanel.add(personalLand);
		addPanel.add(personalLandEntry);
		addPanel.add(profMobile);
		addPanel.add(profMobileEntry);
		addPanel.add(profLand);
		addPanel.add(profLandEntry);
		addPanel.add(homePage);
		addPanel.add(homePageEntry);
		addPanel.add(emailId);
		addPanel.add(emailIdEntry);
		addPanel.add(photo);
		addPanel.add(photoEntry);
		addPanel.add(date);
		addPanel.add(dateEntry);
		addPanel.add(submitButton4);
		

		getAddframe().add(addPanel);
		getAddframe().setVisible(true);

	}

	// DELETE
	static JPanel delPanel = new JPanel();
	private static JFrame delframe = new JFrame("Delete Contact");
	public static void createFrame2(){
		getDelframe().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getDelframe().setSize(600, 200);
		getDelframe().setLocationRelativeTo(null);
		
		delPanel.add(fName);
		delPanel.add(fNameEntry);
		delPanel.add(lName);
		delPanel.add(lNameEntry);
		delPanel.add(submitButton3);
		
		getDelframe().add(delPanel);
		getDelframe().setVisible(true);	
		
    }
	
	//EDIT
	public static JFrame createFrame3(){
		JFrame frame = new JFrame("Edit Contact");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(600, 200);
		frame.setLocationRelativeTo(null);
		
		calcPanel.add(fName);
		calcPanel.add(fNameEntry);
		calcPanel.add(lName);
		calcPanel.add(lNameEntry);
		calcPanel.add(submitButton);
		
		frame.add(calcPanel);
		frame.setVisible(true);
		return frame;
	}
	public static void disposeframe(JFrame frame)
	{
		frame.dispose();
	}
	public static JFrame createFrame_3(Contact cont){
		System.out.println(cont.get_fName());
		JFrame frame = new JFrame("Edit Details");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(600, 200);
		frame.setLocationRelativeTo(null);	
		
		
		fNameEntry.setText(cont.get_fName());
		lNameEntry.setText(cont.get_lName());
		groupEntry.setText(cont.get_group());
		homeAddEntry.setText(cont.get_homeAdd());
		compAddEntry.setText(cont.get_compAdd());
		personalMobileEntry.setText(cont.get_personalMobile());
		personalLandEntry.setText(cont.get_personalLand());
		profMobileEntry.setText(cont.get_profMobile());
		profLandEntry.setText(cont.get_profLand());
		homePageEntry.setText(cont.get_homePage());
		emailIdEntry.setText(cont.get_emailId());
		photoEntry.setText(cont.get_photo());
		dateEntry.setText(cont.get_date());
		
		calcPanel.add(fName);
		calcPanel.add(fNameEntry);
		calcPanel.add(lName);
		calcPanel.add(lNameEntry);
		calcPanel.add(group);
		calcPanel.add(groupEntry);
		calcPanel.add(homeAdd);
		calcPanel.add(homeAddEntry);
		calcPanel.add(compAdd);
		calcPanel.add(compAddEntry);
		calcPanel.add(personalMobile);
		calcPanel.add(personalMobileEntry);
		calcPanel.add(personalLand);
		calcPanel.add(personalLandEntry);
		calcPanel.add(profMobile);
		calcPanel.add(profMobileEntry);
		calcPanel.add(profLand);
		calcPanel.add(profLandEntry);
		calcPanel.add(homePage);
		calcPanel.add(homePageEntry);
		calcPanel.add(emailId);
		calcPanel.add(emailIdEntry);
		calcPanel.add(photo);
		calcPanel.add(photoEntry);
		calcPanel.add(date);
		calcPanel.add(dateEntry);
		calcPanel.add(submitButton1);
		frame.add(calcPanel);
		//frame.add(list);
//		frame.add(scrollPane);
		frame.setVisible(true);	
		return frame;
	}
	
	
	//SORT NAME
static JPanel calcPanel;
static JScrollPane scrollPane;

	public static void createFrame4(){
		JFrame frame = new JFrame("Sort by Names");
		calcPanel = new JPanel();
		scrollPane = new JScrollPane();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(600, 200);
		frame.setLocationRelativeTo(null);
		
		calcPanel.add(letter);
		calcPanel.add(letterEntry);
		calcPanel.add(submitButton2);
		
		frame.add(calcPanel);
		frame.setVisible(true);		
	}
	
	//SORT GROUP
	public static void createFrame5(){
		JFrame frame = new JFrame("Sort by Group");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(600, 200);
		frame.setLocationRelativeTo(null);
		
		calcPanel.add(group);
		calcPanel.add(groupEntry);
		calcPanel.add(submitButton);
		
		frame.add(calcPanel);
		frame.setVisible(true);		
	}
	

	public static void createFrame_5(ArrayList<String> data){
		
		JFrame frame = new JFrame("Sort by Group");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(200, 300);
		frame.setLocationRelativeTo(null);		
		
		JScrollPane scrollPane = new JScrollPane();
		JList list = new JList(data.toArray());
		scrollPane.setViewportView(list);
		
		frame.add(calcPanel);
		//frame.add(list);
		frame.add(scrollPane);
		frame.setVisible(true);		
	}
	
	

	//SORT DATE
	public static void createFrame6(){
		JFrame frame = new JFrame("Sort by Date");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(600, 200);
		frame.setLocationRelativeTo(null);
		
		frame.add(calcPanel);
		frame.setVisible(true);		
	}
	
	//DISPLAY 
	public static void createFrame7(){
		JFrame frame = new JFrame("Display Contact");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(600, 200);
		frame.setLocationRelativeTo(null);
		
		calcPanel.add(fName);
		calcPanel.add(fNameEntry);
		calcPanel.add(lName);
		calcPanel.add(lNameEntry);
		calcPanel.add(submitButton);
		
		frame.add(calcPanel);
		frame.setVisible(true);	
	}
	
//SORT DATE
	public static void createFrame_date(ArrayList<Contact> data){
		
		JFrame frame = new JFrame("Sort by Date");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(200, 300);
		frame.setLocationRelativeTo(null);		
		
		JScrollPane scrollPane = new JScrollPane();
		
		ArrayList<String> obj = new ArrayList<String>();
		
		for(Contact c: data){
			obj.add(new StringBuilder().append(c.get_fName()).append(" ").append(c.get_lName()).append("    ").append(c.get_date()).toString());
		}
		final JList list = new JList(obj.toArray());
		scrollPane.setViewportView(list);
		
		frame.add(calcPanel);
		//frame.add(list);
		frame.add(scrollPane);
		frame.setVisible(true);	
	 
		list.addListSelectionListener(
			 new ListSelectionListener(){
				 public void valueChanged(ListSelectionEvent event)
				 {
					
					 Model obj = new Model();
					ArrayList<String> cont = new ArrayList<String>();
					Object name = list.getSelectedValue();
					String[] nm0 = name.toString().split("   ");
					String[] nm = name.toString().split(" ");
					try {
						cont = obj.displayContact(nm[0], nm[1]);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//System.out.println(cont);
					if (!event.getValueIsAdjusting()){NewFrame.createFrame_7(cont);}
				 }
		 
	 });
	}
	public static void createFrame_sortbyName(ArrayList<String> data){
	
//		
		
		if (data.size() > 0 ){
		final JList list = new JList(data.toArray());
		scrollPane.setViewportView(list);
	//		frame.add(calcPanel);
		
		calcPanel.add(scrollPane, BorderLayout.SOUTH);
		calcPanel.updateUI();
		
		 list.addListSelectionListener(
				 new ListSelectionListener(){
					 public void valueChanged(ListSelectionEvent event)
					 {
						
						 Model obj = new Model();
						ArrayList<String> cont = new ArrayList<String>();
						Object name = list.getSelectedValue();
						String[] nm = name.toString().split(" ");
						try {
							cont = obj.displayContact(nm[0], nm[1]);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//System.out.println(cont);
						if (!event.getValueIsAdjusting()){NewFrame.createFrame_7(cont);}
					 }
			 
		 });
		}
		else{JOptionPane.showMessageDialog(null, "No records found! Try another alphabet");}
	
		
	}

	//DISPLAY and FIRE
	public static void createFrame_7(ArrayList<String> data){
		
		JFrame frame = new JFrame("Display Details");
		JPanel calcPanel = new JPanel();
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setSize(250, 300);
		frame.setLocationRelativeTo(null);		
		
		JScrollPane scrollPane = new JScrollPane();
		list = new JList(data.toArray());
		scrollPane.setViewportView(list);
		
		frame.add(calcPanel);
		//frame.add(list);
		frame.add(scrollPane);
		frame.setVisible(true);	
		
		 list.addListSelectionListener(
				 new ListSelectionListener(){
					 public void valueChanged(ListSelectionEvent event)
					 {
						 StringBuilder sb = new StringBuilder();
						 StringBuilder sb2 = new StringBuilder();
						 
						 if (list.getSelectedIndex() == 10 && !event.getValueIsAdjusting())
						 {
							 try {
									Desktop.getDesktop().mail(new URI( sb.append("mailto:").append(list.getSelectedValue().toString()).toString().replaceAll("Email id :     ", "")));
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (URISyntaxException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} 
						 }
							
						
						if (list.getSelectedIndex() == 9 && !event.getValueIsAdjusting())
						{ 
							try {
								Desktop.getDesktop().browse(new URI( sb2.append(list.getSelectedValue().toString()).toString().replaceAll("Home Page Link :     ", "")));
						} catch (IOException | URISyntaxException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					 }
						
					}
						
		 });
	}

	public static JFrame getAddframe() {
		return addframe;
	}

	public static void setAddframe(JFrame addframe) {
		NewFrame.addframe = addframe;
	}

	public static JFrame getDelframe() {
		return delframe;
	}

	public static void setDelframe(JFrame delframe) {
		NewFrame.delframe = delframe;
	}
	
}
